app.controller('payController',function($scope,$location,payService){
	
	//统一下单（生成二维码）
	$scope.createNative=function(){
		payService.createNative().success(
			function(response){
				$scope.out_trade_no=response.out_trade_no;//订单号
				$scope.money=(response.total_fee/100).toFixed(2);//金额(元)
				//生成二维码图片
				var qr=new QRious({
					element:document.getElementById('qrious'),
					size:300,
					level:"H",
					value:response.code_url					
				});
				
				queryOrderStatus(response.out_trade_no);//查询订单状态
				
			}		
		);
		
	}
	
	//查询订单
	queryOrderStatus=function(out_trade_no){
		payService.queryOrderStatus(out_trade_no).success(
			function(response){
				if(response.success){
					//成功页
					location.href="paysuccess.html#?money="+$scope.money;
				}else{
					if(response.message=='二维码超时'){
						$scope.createNative();
					}else{
						//失败页
						location.href="payfail.html";
					}
				}
				
			}
		);
		
	}	
	
	//获取传递过来的参数（金额）
	$scope.getMoney=function(){
		return $location.search()['money'];		
	}
	
	
	
});